﻿using System;
using System.Data;
using System.Linq;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class ClientInstitutionSubMenu : Template.SubMenu
    {
        private readonly IClientInstitutionSubMenu _view;
        private readonly Crud _taskInstitution;
        private DataTable _institution;
        private UInt64 _clientId;

        public ClientInstitutionSubMenu(IClientInstitutionSubMenu view) : this(view, new Task.Context.ClientInstitution()) { }

        public ClientInstitutionSubMenu(IClientInstitutionSubMenu view, Crud task)
        {
            _view = view;
            Task = task;
            _taskInstitution = new Task.Context.Institution();
            FormView = _view;
            Name = "Client Institution";
        }

        public override void Initialize()
        {
            base.Initialize();
            _taskInstitution.OnGetActive += TaskInstitutionOnGetActive;
            _taskInstitution.GetActive();
        }

        void TaskInstitutionOnGetActive(object source, FormEvent e)
        {
            _institution = (DataTable)e.Value;

            if (!e.IsSuccess)
                return;

            var a =
                (_institution.AsEnumerable().Select(
                    b => string.Format("{0}-{1}", b.Field<string>("name"), b.Field<string>("code")))).ToArray();
            _view.FillInstitution(a);

            if (Data == null)
                return;

            for (var i = 0; i < _institution.Rows.Count; i++)
            {
                if (!_institution.Rows[i]["id"].ToString().Equals(Data["institution_id"].ToString())) continue;
                _view.IndexInstitution = i;
                break;
            }
        }

        protected override void SetUi()
        {
            _view.Department = Data["department"].ToString();
            _view.Employed = Convert.ToBoolean(Data["is_employed"].ToString());
            _view.Salary = Convert.ToDecimal(Data["salary"].ToString());
            _view.Position = Data["position"].ToString();
        }

        protected override void FillData()
        {
            Data["client_id"] = _clientId;
            Data["institution_id"] = Convert.ToUInt64(_institution.Rows[_view.IndexInstitution]["id"].ToString());
            Data["department"] = _view.Department;
            Data["is_employed"] = _view.Employed;
            Data["salary"] = _view.Salary;
            Data["position"] = _view.Position;
            Data["user_create_id"] = ToUInt64(Config.User["id"]);
        }

        protected override bool Validate()
        {
            if (_view.IndexInstitution.Equals(-1))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty institution.") });
                _view.FocusInstitution();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Department.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty department.") });
                _view.FocusDepartment();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }

        public void SetClientId(UInt64 clientId)
        {
            _clientId = clientId;
        }
    }
}
